trigger DNC_IDNC_maskingPhoneNumbersonAccount on Account (before insert,before update) {
	//Demo by vinoth
    Boolean UserPermission = False;
    User integrationuser=[select profileid from user where username='westintegration@apolloglobalwest.com.wiudev'];//added by metasts
    List<Case> caseToCreate=new List<Case>();//added by metasts
    RecordType RecId=[select Id from RecordType where Name='West - Support'];//added by metasts
    String RecTypId=String.valueOf(RecId.Id);//added by metasts
    // Below code if Profile Ids are used
    Set<String> ProfileIds = new Set<String>();
    for (UserAccessDNC__c UA_DNC : UserAccessDNC__c.getAll().values()){
        ProfileIds.add(UA_DNC.Profile_Ids__c.substring(0, 15));
    } 
    if(!ProfileIds.isEmpty()){
       UserPermission = ProfileIds.contains(UserInfo.getProfileId().substring(0, 15));
    }          
/*    Schema.DescribeSObjectResult d = Schema.SObjectType.Account;
    Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName =  rtMapByName.get('West - Student Account');
    String RecTypeId  = rtByName.getRecordTypeId();
/*
    // Below code if USer Ids are used
    Set<String> UserIds = new Set<String>();
    for (UserAccessDNC__c UA_DNC : UserAccessDNC__c.getAll().values()){
        if(UA_DNC.User_Ids__c.length()==18){
            UserIds.add(UA_DNC.User_Ids__c);
        } else if(UA_DNC.User_Ids__c.length()==15){
            UserIds.add(Util.generate18CharId(UA_DNC.User_Ids__c));
        }        
    } 
    if(!UserIds.isEmpty()){
        UserPermission = UserIds.contains(UserInfo.getUserId());
    }          
*/
    
    //Code ended by Metasts
    
    for(Account A: Trigger.New){
    if(trigger.isUpdate ){
    //add by metasts 
            if(Trigger.oldMap.get(A.Id).Home_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Home_Phone_Status__pc == 'DNC' && integrationuser.profileid==UserInfo.getProfileId()) {
                If(Trigger.newMap.get(A.Id).PersonHomePhone != Null){
                Case c=new Case(RecordTypeId=RecTypId,AccountId=A.id,Status='New',Origin='Web',Subject='IDNC/DNC occurs',Description='IDNC/DNC',Group__c='Student',Category__c='Admissions',Activity__c='Application',Case_Name__c='PRE Eval');
                              
                caseToCreate.add(c);
                }   
                }
                //end by metasts 
        
        //  Home Phone Masking:
        
           if(integrationuser.profileid!=UserInfo.getProfileId())//code added by metasts for override DNC and IDNC rule for QuickApp flow data
            {
          if(Trigger.oldMap.get(A.Id).Home_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Home_Phone_Status__pc == 'DNC') {
              IF(Trigger.newMap.get(A.Id).PersonHomePhone != Null){A.addError('You cannot be changed "Phone" once it\'s status is marked as DNC/IDNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Home_Phone_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Home_Phone_Status__pc == 'DNC') {
                A.PersonHomePhone='';                           
            } else if(Trigger.oldMap.get(A.Id).Home_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Home_Phone_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC/IDNC permissions can change "DNC" once it\'s status is marked as DNC/IDNC!');
                } else {
                    A.PersonHomePhone = A.Home_Phone_Unmasked__pc;
                }                
            } else {
                A.Home_Phone_Unmasked__pc= A.PersonHomePhone;
            }
            
            
                   

        //  Mobile Phone Masking:
            if(Trigger.oldMap.get(A.Id).Mobile_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Mobile_Phone_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).PersonMobilePhone != Null){ A.addError('You cannot be changed "Mobile" once it\'s status is marked as DNC/IDNC!');}                          
            } else if(Trigger.oldMap.get(A.Id).Mobile_Phone_Status__pc != 'DNC' &&  Trigger.newMap.get(A.Id).Mobile_Phone_Status__pc == 'DNC') {
                A.PersonMobilePhone='';                           
            } else if(Trigger.oldMap.get(A.Id).Mobile_Phone_Status__pc == 'DNC' &&  Trigger.newMap.get(A.Id).Mobile_Phone_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC/IDNC permissions can change "DNC" once it\'s status is marked as DNC/IDNC!');
                } else {
                    A.PersonMobilePhone = A.Mobile_Phone_Unmasked__pc;
                }                
            } else  {
                A.Mobile_Phone_Unmasked__pc= A.PersonMobilePhone;
            }        

        //  Mother Phone Masking:
            if(Trigger.oldMap.get(A.Id).Mother_s_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Phone_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Mother_s_Phone__pc != Null){A.addError('You cannot be changed "Mother\'s Phone" once it\'s status is marked as DNC/IDNC!');}                          
            } else if(Trigger.oldMap.get(A.Id).Mother_s_Phone_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Phone_Status__pc == 'DNC') {
                A.Mother_s_Phone__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Mother_s_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Phone_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC/IDNC permissions can change "DNC" once it\'s status is marked as DNC/IDNC!');
                } else {
                    A.Mother_s_Phone__pc = A.Mother_s_Phone_Unmasked__pc;
                }                
            } else {
                A.Mother_s_Phone_Unmasked__pc= A.Mother_s_Phone__pc;
            }        

        //  Father Phone Masking:
            if(Trigger.oldMap.get(A.Id).Father_s_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Father_s_Phone_Status__pc == 'DNC'){
                IF(Trigger.newMap.get(A.Id).Father_s_Phone__pc != Null){A.addError('You cannot be changed "Father\'s Phone" once it\'s status is marked as DNC/IDNC!');}                          
            } else if(Trigger.oldMap.get(A.Id).Father_s_Phone_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Father_s_Phone_Status__pc == 'DNC') {
                A.Father_s_Phone__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Father_s_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Father_s_Phone_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC/IDNC permissions can change "DNC" once it\'s status is marked as DNC/IDNC!');
                } else {
                    A.Father_s_Phone__pc = A.Father_s_Phone_Unmasked__pc;
                }                
            } else {
                A.Father_s_Phone_Unmasked__pc= A.Father_s_Phone__pc;
            }        

        //  Marketing Phone Masking:
            if(Trigger.oldMap.get(A.Id).Marketing_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Marketing_Phone_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Marketing_Phone__pc != Null){A.addError('You cannot be changed "Marketing Phone" once it\'s status is marked as DNC/IDNC!');}                          
            } else if(Trigger.oldMap.get(A.Id).Marketing_Phone_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Marketing_Phone_Status__pc == 'DNC') {
                A.Marketing_Phone__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Marketing_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Marketing_Phone_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC/IDNC permissions can change "DNC" once it\'s status is marked as DNC/IDNC!');
                } else {
                    A.Marketing_Phone__pc = A.Marketing_Phone_Unmasked__pc;
                }                
            } else {
                A.Marketing_Phone_Unmasked__pc= A.Marketing_Phone__pc;
            }        

        //  Other Phone Masking:
            if(Trigger.oldMap.get(A.Id).Other_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).PersonOtherPhone != Null){A.addError('You cannot be changed "Other Phone" once it\'s status is marked as DNC/IDNC!');}                          
            } else if(Trigger.oldMap.get(A.Id).Other_Phone_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_Status__pc == 'DNC') {
                A.PersonOtherPhone='';                           
            } else if(Trigger.oldMap.get(A.Id).Other_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC/IDNC permissions can change "DNC" once it\'s status is marked as DNC/IDNC!');
                } else {
                    A.PersonOtherPhone = A.Other_Phone_Unmasked__pc;
                }                
            } else {
                A.Other_Phone_Unmasked__pc= A.PersonOtherPhone;
            }        
        
        //  Other Phone 2 Masking:
            if(Trigger.oldMap.get(A.Id).Other_Phone_2_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_2_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Other_Phone_2__pc != Null){A.addError('You cannot be changed "Other Phone 2" once it\'s status is marked as DNC/IDNC!');}                          
            } else if(Trigger.oldMap.get(A.Id).Other_Phone_2_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_2_Status__pc == 'DNC') {
                A.Other_Phone_2__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Other_Phone_2_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_2_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC/IDNC permissions can change "DNC" once it\'s status is marked as DNC/IDNC!');
                } else {
                    A.Other_Phone_2__pc = A.Other_Phone_2_Unmasked__pc;
                }                
            } else {
                A.Other_Phone_2_Unmasked__pc= A.Other_Phone_2__pc;
            }        

        //  Work Phone Masking:
            if(Trigger.oldMap.get(A.Id).Work_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Work_Phone_Status__pc == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Work_Phone__pc != Null){A.addError('You cannot be changed "Work Phone" once it\'s status is marked as DNC/IDNC!');}                          
            } else if(Trigger.oldMap.get(A.Id).Work_Phone_Status__pc != 'DNC' && Trigger.newMap.get(A.Id).Work_Phone_Status__pc == 'DNC') {
                A.Work_Phone__pc='';                           
            } else if(Trigger.oldMap.get(A.Id).Work_Phone_Status__pc == 'DNC' && Trigger.newMap.get(A.Id).Work_Phone_Status__pc != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with DNC/IDNC permissions can change "DNC" once it\'s status is marked as DNC/IDNC!');
                } else {
                    A.Work_Phone__pc = A.Work_Phone_Unmasked__pc;
                }                
            } else {
                A.Work_Phone_Unmasked__pc= A.Work_Phone__pc;
            }        
            }
            insert caseToCreate;//added by metasts
    }
    else if(trigger.isInsert ){

        //  Home Phone Masking:
            if(A.Home_Phone_Status__pc == 'DNC') {
                    A.Home_Phone_Unmasked__pc= A.PersonHomePhone;
                    A.PersonHomePhone='';                           
            } else {
                A.Home_Phone_Unmasked__pc= A.PersonHomePhone;
            }        
        
        //  Mobile Phone Masking:
            if(A.Mobile_Phone_Status__pc == 'DNC') {
                    A.Mobile_Phone_Unmasked__pc= A.PersonMobilePhone;
                    A.PersonMobilePhone='';                           
            } else {
                A.Mobile_Phone_Unmasked__pc= A.PersonMobilePhone;
            }        
        
        //  Mother Phone Masking:
            if(A.Mother_s_Phone_Status__pc == 'DNC') {
                    A.Mother_s_Phone_Unmasked__pc= A.Mother_s_Phone__pc;
                    A.Mother_s_Phone__pc='';                           
            } else {
                A.Mother_s_Phone_Unmasked__pc= A.Mother_s_Phone__pc;
            }        
        
        //  Father Phone Masking:
            if(A.Father_s_Phone_Status__pc == 'DNC') {
                    A.Father_s_Phone_Unmasked__pc= A.Father_s_Phone__pc;
                    A.Father_s_Phone__pc='';                           
            } else {
                A.Father_s_Phone_Unmasked__pc= A.Father_s_Phone__pc;
            }        
        
        //  Marketing Phone Masking:
            if(A.Marketing_Phone_Status__pc == 'DNC')  {
                    A.Marketing_Phone_Unmasked__pc= A.Marketing_Phone__pc;
                    A.Marketing_Phone__pc='';                           
            } else {
                A.Marketing_Phone_Unmasked__pc= A.Marketing_Phone__pc;
            }        
        
        //  Other Phone Masking:
            if(A.Other_Phone_Status__pc == 'DNC') {
                    A.Other_Phone_Unmasked__pc= A.PersonOtherPhone;
                    A.PersonOtherPhone='';                           
            } else {
                A.Other_Phone_Unmasked__pc= A.PersonOtherPhone;
            }        
        
        //  Other Phone 2 Masking:
            if(A.Other_Phone_2_Status__pc == 'DNC') {
                    A.Other_Phone_2_Unmasked__pc= A.Other_Phone_2__pc;
                    A.Other_Phone_2__pc='';                           
            } else {
                A.Other_Phone_2_Unmasked__pc= A.Other_Phone_2__pc;
            }        
        
        //  Work Phone Masking:
            if(A.Work_Phone_Status__pc == 'DNC') {
                    A.Work_Phone_Unmasked__pc= A.Work_Phone__pc;
                    A.Work_Phone__pc='';                           
            } else {
                A.Work_Phone_Unmasked__pc= A.Work_Phone__pc;
            }        
    }
    }
}